﻿Public Class FrmNhanVien

    Private Sub FrmNhanVien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDuLieu()
    End Sub
    Dim provider As New DataProvider
    Dim flag As String = 0
    Dim sql As String

    Private Sub LoadDuLieu()
        Dim sql As String
        sql = "SELECT nv.MaNV, nv.TenNV,bp.TenBP,cv.TenCV FROM NhanVien nv,BoPhan bp, ChucVu cv WHERE nv.MaBP = bp.MaBP AND nv.MaCV = cv.MaCV"
        dtgnhanvien.DataSource = provider.ExecuteQuery(sql)
        Enable(False)
        btnluu.Enabled = False
        btnhuy.Enabled = False
        Enable(False)
        btnthem.Enabled = True
        btnsua.Enabled = True
        cbxbophan.Enabled = False
        cbxchucvu.Enabled = False
        LoadBoPhan()
        LoadChucVu()

    End Sub
    Private Sub LoadChucVu()
        sql = "select * from ChucVu"
        cbxchucvu.DataSource = provider.ExecuteQuery(sql)
        cbxchucvu.DisplayMember = "TenCV"
        cbxchucvu.ValueMember = "MaCV"
    End Sub
    Private Sub LoadBoPhan()
        sql = "select * from BoPhan"
        cbxbophan.DataSource = provider.ExecuteQuery(sql)
        cbxbophan.DisplayMember = "TenBP"
        cbxbophan.ValueMember = "MaBP"
    End Sub


    Private Sub dtgnhanvien_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtgnhanvien.CellClick
        Dim row As Integer
        Dim sql As String
        Dim dt As New DataTable
        row = dtgnhanvien.CurrentCell.RowIndex
        txtmanv.Text = dtgnhanvien.Rows(row).Cells(0).Value
        txttennv.Text = dtgnhanvien.Rows(row).Cells(1).Value
        sql = "SELECT * FROM NhanVien nv,BoPhan bp, ChucVu cv WHERE nv.MaBP = bp.MaBP AND nv.MaCV = cv.MaCV and  nv.MaNV='" + dtgnhanvien.Rows(row).Cells(0).Value + "'"
        dt = provider.ExecuteQuery(sql)
        cbxbophan.Text = dt.Rows(0)(5).ToString().Trim
        cbxchucvu.Text = dt.Rows(0)(7).ToString().Trim
    End Sub

    Private Sub btnthem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthem.Click
        txtmanv.Text = ""
        txttennv.Text = ""
        btnsua.Enabled = False
        Enable(True)
        flag = 1
        btnhuy.Enabled = True
        btnluu.Enabled = True
        flag = 1
        btnthem.Enabled = False

        cbxbophan.Enabled = True
        cbxchucvu.Enabled = True
    End Sub
    Private Sub Enable(ByVal enable As Boolean)
        txtmanv.Enabled = enable
        txttennv.Enabled = enable

    End Sub

    Private Sub btnhuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnhuy.Click
        LoadDuLieu()
    End Sub

    Private Sub btnsua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsua.Click

        If txtmanv.Text = "" Then
            MessageBox.Show("Chọn 1 Nhân Viên để sửa")

        Else
            flag = 2
            btnhuy.Enabled = True
            btnluu.Enabled = True
            btnthem.Enabled = False
            btnsua.Enabled = False
            Enable(True)
            'LoadBoPhan()
            'LoadChucVu()
            cbxbophan.Enabled = True
            cbxchucvu.Enabled = True
        End If


    End Sub


    Private Sub btnluu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnluu.Click
        Try

            If flag = 1 Then
                'Thêm
                Dim manv As String
                Dim tennv As String
                Dim mabp As String
                Dim macv As String
                manv = txtmanv.Text.Trim
                tennv = txttennv.Text
                mabp = cbxbophan.SelectedValue
                macv = cbxchucvu.SelectedValue

                sql = "select * from nhanvien where manv='" + manv + "'"
                Dim dt As DataTable
                dt = provider.ExecuteQuery(sql)

                If dt.Rows.Count > 0 Then
                    MessageBox.Show("Mã nhân viên này đã tồn tại,")
                    txtmanv.Focus()
                Else
                    sql = "INSERT INTO NhanVien(MaNV,TenNV,MaBP,MaCV)VALUES(N'" + manv + "',N'" + tennv + "','" + mabp + "','" + macv + "')"
                    provider.ExecuteNonQuery(sql)
                    MessageBox.Show(" Thêm Nhân Viên Thành Công")

                End If

            ElseIf flag = 2 Then
                Dim manv As String
                Dim tennv As String
                Dim mabp As String
                Dim macv As String
                manv = txtmanv.Text.Trim
                tennv = txttennv.Text
                mabp = cbxbophan.SelectedValue
                macv = cbxchucvu.SelectedValue
                sql = "UPDATE NhanVien SET	MaNV = N'" + manv + "',	TenNV = N'" + tennv + "',	MaBP = '" + mabp + "',	MaCV = '" + macv + "' where manv='" + manv + "'"
                Dim i As Integer
                provider.ExecuteQuery(sql)

                MessageBox.Show("Chỉnh sửa thành công")
            End If
            LoadDuLieu()
        Catch ex As Exception

        End Try
    End Sub
End Class